package com.nf.health.app.main_intervention.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.http.Act;
import com.nf.health.app.main_intervention.bean.PlayMusic;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.music.MusicPlay;
import com.nf.health.app.widget.DialogFactory;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 执行方案： music
 *
 * @author yuyahao
 */
public class MusicActivity extends BaseActivity {
    private final int MUSIC_FINISH = 1000;
    @Bind(R.id.tv_suggest_time)
    TextView tv_suggest_time;
    @Bind(R.id.iv_bg_music)
    ImageView iv_bg_music;
    @Bind(R.id.titlebar_back_music)
    ImageView titlebar_back_music;
    @Bind(R.id.titlebar_title_music)
    TextView titlebar_title_music;
    @Bind(R.id.ib_paly_music)//播放音乐
    ImageButton ib_paly_music;
    @Bind(R.id.ll_to_play)//播放音乐
     LinearLayout ll_to_play;
    @Bind(R.id.tv_time_progress)//正在播放的时间
            TextView tv_time_progress;
    @Bind(R.id.tv_time_totle)//播放总时间
            TextView tv_time_totle;
    @Bind(R.id.sb_music_progress)//播放拖动条
            SeekBar sb_music_progress;
    @Bind(R.id.psb_progressBar)//下载进度条
            ProgressBar psb_progressBar;
    private MusicPlay mausicPlay;
    private DialogFactory dialogFactory;
    private SimpleDateFormat formatter;
    private String   MusicUrl = "";
    private String ID = "";
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MUSIC_FINISH:
                    tv_time_progress.setText("0:00");//播放完毕后，重新设置没有播放的状态
                    sb_music_progress.setProgress(0);
                    ib_paly_music.setBackgroundResource(R.drawable.iv_paly);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_paly);
        ButterKnife.bind(this);
        initListener();
        initView();
    }

    private void initView() {
        formatter = new SimpleDateFormat("mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
//        Util.settranslucentStatusBar(this);
        GlideUtil.loadLocalImgC(MusicActivity.this,iv_bg_music,R.drawable.tv_music_bg);
        mausicPlay = MusicPlay.getInstance(this).setMySeekListener(sb_music_progress)
                .setMySeekProgressListener(new MusicPlay.MySeekProgressListener() {
                    @Override
                    public void onMySeekProgressData(String totleTime, String currentTime,int finishStatus) {
                        if(finishStatus == 100){//播放完毕
                            LogUtil.e("yuyahao","mian 时间 放放完啦！！！ ");
                        }else{
                            tv_time_totle.setText(totleTime);
                            if(totleTime.equals(currentTime)){//用时间字符串进行比较，防止出现两次的时间结束状态
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Message msg = handler.obtainMessage();
                                        msg.what = MUSIC_FINISH;
                                        handler.sendMessage(msg);
                                    }
                                }, 800);
                            }else{
                                tv_time_totle.setText(totleTime);
                                tv_time_progress.setText(currentTime);
                            }
                           // LogUtil.e("yuyahao","mian_    totle:  " +totleLong1+"当前：  " + currentLong1+"    "+totleLong+"   "+currentLong +  "    " +finishStatus);
                        }
                    }
                });
        ib_paly_music.setTag(R.drawable.iv_stop_paly);
        ib_paly_music.setBackgroundResource(R.drawable.iv_paly);
        ID =getIntent().getStringExtra(Constants.INTERVENTION_ID);
    }

    private void initListener() {
        ib_paly_music.setOnClickListener(this);
        titlebar_back_music.setOnClickListener(this);
        ll_to_play.setOnClickListener(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        loadData(ID);
    }
    private void loadData(String id){
        provider.getInterveneMusicById(id);
    }
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.GETINTERVENEMUSICBYID_FLAG:
                PlayMusic playMusic = (PlayMusic) object;
                if(playMusic != null){
                    titlebar_title_music.setText(TextUtils.isEmpty(playMusic.getName()) ? "" :playMusic.getName());
                    tv_suggest_time.setText(TextUtils.isEmpty(playMusic.getTime()) ? "建议时间：暂无" : "建议时间：" +playMusic.getTime()+" "+playMusic.getHour());
                    GlideUtil.loadNetImgC(MusicActivity.this,iv_bg_music,playMusic.getPicture(),R.drawable.tv_music_bg);
                    MusicUrl = playMusic.getUrl();
                    mausicPlay.setURLMUSIC(MusicUrl);
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_to_play:
            case R.id.ib_paly_music://博faNG或者是暂停
                Integer tag = (Integer) ib_paly_music.getTag();

                if(mausicPlay.isPlayIng()){//正在播放，去暂停播放
                    ib_paly_music.setBackgroundResource(R.drawable.iv_paly);
                }else{//去进行播放
                    ib_paly_music.setBackgroundResource(R.drawable.iv_stop_paly);
                }
                mausicPlay.play();
                break;
            case R.id.titlebar_back_music:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mausicPlay != null){
            mausicPlay.onDestroy();
        }
    }

    @Override
    public void onBackPressed() {
         if(mausicPlay.isPlayIng()){
             if(dialogFactory == null){
                 dialogFactory = new DialogFactory(MusicActivity.this);
                 dialogFactory.setCancelable(true);
                 dialogFactory.setMsg("温馨提示");
                 dialogFactory.setButtonText(R.id.btn_left,"取消");
                 dialogFactory.setButtonText(R.id.btn_middle,"确定");
                 dialogFactory.setMsg("当前音乐尚未播放完毕，是否停止播放");
                 dialogFactory.setButtonOnClickListener(R.id.btn_left, new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         dialogFactory.dismiss();
                     }
                 });
                 dialogFactory.setButtonOnClickListener(R.id.btn_middle, new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         finish();
                     }
                 });
                 dialogFactory.show();
             }else{
                 dialogFactory.show();
             }

         }else{
            super.onBackPressed();
         }
    }
}
